package com.ad.pet.app.recharge;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.common.data.utils.SpUtil;
import com.ad.pet.common.data.utils.ui.DialogUtil;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.account.CountDrawTotal;
import com.ad.pet.models.remote.recharge.WithDraw;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by SEELE on 2019/6/1.
 */

public class CashMoneyActivity extends AppToolbarActivity implements CashMoneyContracts.View {


    @BindView(R.id.tv_price)
    TextView mTvPrice;
    @BindView(R.id.tv_cash_money)
    TextView mTvCashMoney;
    @BindView(R.id.et_price)
    EditText mEtPrice;
    @BindView(R.id.tv_method)
    TextView mTvMethod;
    @BindView(R.id.et_number)
    EditText mEtNumber;
    @BindView(R.id.et_username)
    EditText mEtUsername;
    @BindView(R.id.btn_commit)
    Button mBtnCommit;
    @BindView(R.id.btn_cancel)
    Button mBtnCancel;

    @Inject
    CashMoneyPresenter mCashMoneyPresenter;

    private WithDraw mWithDraw;
    int drawPositon = -1;



    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
        mToolbar.setTitle(getString(R.string.back_charge));
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_cash_money;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mCashMoneyPresenter.onCreate(this);
        mCashMoneyPresenter.getWithDraw();


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        mTvMethod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mWithDraw == null)
                    return;
                final String[] strings = new String[mWithDraw.getWithdraw_type_list().size()];
                for (int i = 0; i < mWithDraw.getWithdraw_type_list().size(); i++) {
                    strings[i] = mWithDraw.getWithdraw_type_list().get(i).getName();
                }

                DialogUtil.createSingleChoiceItemsDialog(mActivity, mTvMethod.getHint().toString(), strings, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        drawPositon = which;
                        mTvMethod.setText(strings[which]);
                        dialog.dismiss();
                    }
                }).show();
            }
        });

        mBtnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEtPrice.getText().toString().trim())) {
                    ToastUtil.getToastUtil().showShort(getString(R.string.pleash_write_money));
                    return;
                }
                if (!isNumber(mEtPrice.getText().toString().trim())){
                    ToastUtil.getToastUtil().showShort(getString(R.string.please_write_true_money));
                    return;
                }
                if (TextUtils.isEmpty(mEtPrice.getText().toString().trim())) {
                    ToastUtil.getToastUtil().showShort(getString(R.string.pleash_write_money));
                    return;
                }
                if (drawPositon == -1) {
                    ToastUtil.getToastUtil().showShort(mTvMethod.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(mEtNumber.getText().toString().trim())) {
                    ToastUtil.getToastUtil().showShort(mEtNumber.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(mEtUsername.getText().toString().trim())) {
                    ToastUtil.getToastUtil().showShort(mEtNumber.getHint().toString());
                    return;
                }
                mCashMoneyPresenter.addWithDraw(mWithDraw.getWithdraw_type_list().get(drawPositon).getType(), mEtNumber.getText().toString().trim(), mEtUsername.getText().toString().trim(), mEtPrice.getText().toString());
            }
        });
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTvCashMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goCommonWebView(mActivity, mTvCashMoney.getText().toString(), getString(R.string.ip_default) + "/static/redpackage_use.html?lang=" + SpUtil.getInstance().getString(Constant.LANGUAGE, "cn"));
            }
        });
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return mCashMoneyPresenter;
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {
        ToastUtil.getToastUtil().showShort(msg);
    }

    @Override
    public void getWithDrawSuccess(WithDraw w) {

        mTvPrice.setText(w.getUsd_symbol() + w.getTotal_withdraw_balance());
        mWithDraw = w;

        if (TextUtils.isEmpty(w.getTotal_withdraw_balance()) || Double.valueOf(w.getTotal_withdraw_balance()) == 0) {
            mBtnCommit.setBackgroundResource(R.drawable.shap_grey_white_bg);
            mBtnCommit.setClickable(false);
        }

    }

    @Override
    public void addWithDrawSuccess() {
        ToastUtil.getToastUtil().showShort(getString(R.string.add_withdraw_success));
        finish();
    }

    @Override
    public void getCurrentTotalWithdrawalSuccess(CountDrawTotal countDrawTotal) {

    }

    private static boolean isNumber(String str){
        java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
        java.util.regex.Matcher match=pattern.matcher(str);
        if(match.matches()==false){
            return false;
        }
        else{
            return true;
        }
    }
}
